/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.AlreadyChargeMonthAndItemStatQueryVO;
import com.ce.pms.report.queryvo.ChargeCollectQueryVO;
import com.ce.pms.report.vo.BaseReportVo;
import com.ce.pms.report.vo.ChargeCollectVo;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 * 收费汇总表
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-11-14
 */

public class ChargeCollectService  implements
		ICEReportDataSetService {

	private IReportDao reportDao;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg0)
			throws CEReportException {
		ChargeCollectQueryVO queryVO = (ChargeCollectQueryVO) arg0;
		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述: 获取数据列表 <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<ChargeCollectVo> getData(ChargeCollectQueryVO queryVO){

		List<ChargeCollectVo> list = converToReportVoList(reportDao.getChargeCollect(queryVO));
		return  list;
	}

	/**
	 * 功能描述: 转VO <br>
	 * @param dataList
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private  List<ChargeCollectVo> converToReportVoList(List<Object[]> dataList) {

		if (BlankUtil.isBlank(dataList)) return null;

		List<ChargeCollectVo> voList = new ArrayList<ChargeCollectVo>();

		for (Object[] objTmp : dataList) {
			ChargeCollectVo voTmp = new ChargeCollectVo();

			voTmp.setHouseNumber((String)objTmp[1]);	//房号
			voTmp.setUserName((String) objTmp[3]);		// 客户名称
			voTmp.setBuildingAcreage(setValue(objTmp[4]).doubleValue());// 建筑面积

			// 应收
			voTmp.setOverheadExpenses_Y(setValue(objTmp[5]));	//物业费
			voTmp.setWaterPay_Y(setValue( objTmp[6]));			//水费
			voTmp.setElectricityPay_Y(setValue( objTmp[7]));		//电费
			voTmp.setSewagePay_Y(setValue(objTmp[8]));			//污水处理费
			voTmp.setGarbagePay_Y(setValue(objTmp[9]));			//垃圾费
			voTmp.setOnticUpkeep_Y(setValue( objTmp[10]));		//本体维修基金
			voTmp.setUpkeepPay_Y(setValue(objTmp[11]));			//维修费
			voTmp.setLatefee_Y(setValue(objTmp[12]));			//滞纳金

			//减免,调整
			voTmp.setDerateChargeCount(setValue(objTmp[13]));	 // 减免费用
			voTmp.setAdjustChargeCount(setValue(objTmp[14]));    // 调整费用
			voTmp.setCount_Y(setZeroValue(objTmp[15]));			 //应收合计

			// 实收
			voTmp.setOverheadExpenses_S(setValue( objTmp[19]));	//物业费
			voTmp.setWaterPay_S(setValue(objTmp[20]));			//水费
			voTmp.setElectricityPay_S(setValue(objTmp[21]));	//电费
			voTmp.setSewagePay_S(setValue(objTmp[22]));			//污水处理费
			voTmp.setGarbagePay_S(setValue(objTmp[23]));		//垃圾费
			voTmp.setOnticUpkeep_S(setValue(objTmp[24]));		//本体维修基金
			voTmp.setUpkeepPay_S(setValue(objTmp[25]));			//维修费
			voTmp.setLatefee_S(setValue(objTmp[26]));			//滞纳金

			voTmp.setCount_S(setZeroValue(objTmp[27]));			//实收合计

			// 欠收
			voTmp.setOverheadExpenses_Q(setValue(objTmp[31]));	//物业费
			voTmp.setWaterPay_Q(setValue(objTmp[32]));			//水费
			voTmp.setElectricityPay_Q(setValue(objTmp[33]));	//电费
			voTmp.setSewagePay_Q(setValue(objTmp[34]));			//污水处理费
			voTmp.setGarbagePay_Q(setValue(objTmp[35]));		//垃圾费
			voTmp.setOnticUpkeep_Q(setValue(objTmp[36]));		//本体维修基金
			voTmp.setUpkeepPay_Q(setValue(objTmp[37]));			//维修费
			voTmp.setLatefee_Q(setValue(objTmp[38]));			//滞纳金
			voTmp.setCount_Q(setZeroValue(objTmp[39]));			//欠费合计

			BigDecimal Count_S = voTmp.getCount_S();
			BigDecimal Count_Y = voTmp.getCount_Y();
			BigDecimal Count_Q = voTmp.getCount_Q();
			if (Count_Y.intValue() != 0){
				voTmp.setCallbackRate(Count_S.divide(Count_Y,4,BigDecimal.ROUND_DOWN).doubleValue());
				voTmp.setOweRate(Count_Q.divide(Count_Y,4,BigDecimal.ROUND_DOWN).doubleValue());
			}
			voList.add(voTmp);
		}
		return voList;
	}

	/**
	 * 传入的值必须是 BigDecimal 类型的,否则返回空 如果为空或值为0 则返回空
	 */
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 传入的值必须是 BigDecimal 类型的,否则返回空 如果为空或值为0 则返回0
	 */
	private BigDecimal setZeroValue(Object obj){
		if(BlankUtil.isBlank(obj)){
			return new BigDecimal(0);
		}
		if (obj instanceof BigDecimal) {
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 功能描述:获取头信息  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(ChargeCollectQueryVO queryVO){
		Map<String, Object> map =new HashMap<String, Object>();
		String chargeDate = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyy年MM月");
		map.put("reportHeadStr", "收款月份："+ chargeDate);
		map.put("printDate", new Date());

		String subentry = "";
		if("1".equals(queryVO.getSubentry())){
			subentry="本月费用";
		}else if("2".equals(queryVO.getSubentry())){
			subentry="往月欠款";
		}else{
			subentry="本月费用 + 往月欠款";
		}
		map.put("subentry", subentry);

		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}
}
